package com.halflife.shiro.utils;

import com.halflife.comeElectricity.member.merchant.Merchant;
import com.halflife.comeElectricity.member.rider.Rider;
import com.halflife.comeElectricity.user.User;
import com.halflife.framework.participant.user.UserView;
import org.apache.shiro.SecurityUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhaoyf on 2018/3/7.
 */
public class ShiroUtil {
    public static final String MERCHANT = "merchant";
    public static final String RIDER = "rider";
    public static final String USER = "user";
    /**
     * 测评系统用户
     */
    public static final String USERVIEW ="userView";

    public static void setUserview(UserView userview){

        SecurityUtils.getSubject().getSession().setAttribute(USERVIEW, userview);
    }

    public static  UserView getUserView(HttpServletRequest request){

//        String unionid = request.getParameter("unionid");
        UserView userView = (UserView) SecurityUtils.getSubject().getSession().getAttribute(USERVIEW);
        if (userView == null) {
            throw new RuntimeException("登陆授权异常");
        }
//        if (!unionid.equals(userView.getUnionid())) {
//            throw new RuntimeException("登陆授权异常");
//        }
        return userView;
    }
    public static void setMerchant(Merchant merchant) {
        SecurityUtils.getSubject().getSession().setAttribute(MERCHANT, merchant);
    }

    public static void setRider(Rider rider) {
        SecurityUtils.getSubject().getSession().setAttribute(RIDER, rider);
    }

    public static void setUser(User user) {
        SecurityUtils.getSubject().getSession().setAttribute(USER, user);
    }

    public static Merchant getMerchant(HttpServletRequest request) {
        String unionid = request.getParameter("unionid");
        Merchant merchant = (Merchant) SecurityUtils.getSubject().getSession().getAttribute(MERCHANT);
        if (!unionid.equals(merchant.getUnionid())) {
            throw new RuntimeException("登陆授权异常");
        }
        return merchant;
    }

    public static Rider getRider(HttpServletRequest request) {
        Rider rider = (Rider) SecurityUtils.getSubject().getSession().getAttribute(RIDER);
        String unionid = request.getParameter("unionid");
        if (!unionid.equals(rider.getUnionid())) {
            throw new RuntimeException("登陆授权异常");
        }
        return rider;
    }

    public static User getUser() {
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute(USER);
        return user;
    }
}
